package scu.maqiang.numeric;

import java.util.Arrays;

public class UnorderIntArray {
	public UnorderIntArray(int... x) {
		intSet = x.clone();
	}
	
	public int[] getIntSet() {
		return intSet.clone();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 17;
		int[] other = intSet.clone();
		Arrays.sort(other);
		for(int i = 0, n = intSet.length; i < n; i++) {
			result = prime * result + other[i];
		}
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UnorderIntArray other = (UnorderIntArray) obj;
		
		int[] other2 = intSet.clone();
		Arrays.sort(other2);
		Arrays.sort(other.intSet);
		
		int n = intSet.length;
		if (n != other.intSet.length) {
			return false;
		}
		for(int i = 0; i < n; i++) {
			if (other2[i] != other.intSet[i]) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Returns a string representation of the object. In general, the
	 * {@code toString} method returns a string that
	 * "textually represents" this object. The result should
	 * be a concise but informative representation that is easy for a
	 * person to read.
	 * It is recommended that all subclasses override this method.
	 * <p>
	 * The {@code toString} method for class {@code Object}
	 * returns a string consisting of the name of the class of which the
	 * object is an instance, the at-sign character `{@code @}', and
	 * the unsigned hexadecimal representation of the hash code of the
	 * object. In other words, this method returns a string equal to the
	 * value of:
	 * <blockquote>
	 * <pre>
	 * getClass().getName() + '@' + Integer.toHexString(hashCode())
	 * </pre></blockquote>
	 *
	 * @return a string representation of the object.
	 */
	@Override
	public String toString() {
		return Arrays.toString(intSet);
	}

	private int[] intSet;
}
